//
//  DatabaseTool.m
//  OrderDish_Class
//
//  Created by Smile_zhang on 15/8/30.
//  Copyright (c) 2015年 ZY. All rights reserved.
//

#import "DatabaseTool.h"
#import "sheng.h"
#import "city.h"
@implementation DatabaseTool
static  FMDatabase *_db = nil;


//查询省
+(NSMutableArray *)querySheng
{
    NSMutableArray *array =[[NSMutableArray  alloc] init];
    FMDatabase  *db = [DatabaseTool shareDatabase];
    FMResultSet *set = [db executeQuery:@"select * from ERP_PROVINCE_CODE Order by SORT DESC"];
    while ([set next])
    {
        sheng*sName=[[sheng alloc]initWithShengID:[set stringForColumn:@"PROVINCE_ID"] shengName:[set stringForColumn:@"PROVINCE_NAME"]];
        [array addObject:sName];
    }
    [set close];
    [db close];
    return array;
}



//查询城市
+(NSMutableArray*)queryCity
{
    NSMutableArray *array =[[NSMutableArray  alloc] init];
    FMDatabase  *db = [DatabaseTool shareDatabase];
    FMResultSet *set = [db executeQuery:@"select * from ERP_CITY_CODE Order by SORT DESC"];
    while ([set next])
    {
        city*cName=[[city alloc]initWithCityID:[set stringForColumn:@"CITY_ID"] shengID:[set stringForColumn:@"PROVINCE_ID"] cityName:[set stringForColumn:@"CITY_NAME"]];
        [array addObject:cName];
    }
    [set close];
    [db close];
    return array;
}

+(FMDatabase *)shareDatabase
{
    if (_db == nil)
    {
        NSString*path=[[NSBundle mainBundle]pathForResource:@"city" ofType:@"db"];
        _db = [[FMDatabase alloc] initWithPath:path];
    }
    [self open];
    return _db;
}

+(BOOL)open
{
    if (![_db open])
    {
        [_db close];
        return NO;
    }
    [_db  setShouldCacheStatements:YES];
    return YES;
}

@end
